﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WellDone
{
    public partial class AddPaymentTypeDialog : ChildWindow
    {
        public AddPaymentTypeDialog()
        {
            InitializeComponent();
        }

        // Add Alternate Payment Types
        private async void OKButton_Click(object sender, RoutedEventArgs e)
        {
            ServiceReferenceOpenRPOS.PaymentType paymenttype = new ServiceReferenceOpenRPOS.PaymentType();
            if (tbPaymentTypeKey.Text.Trim().Length>0 && tbPaymentTypeValue.Text.Trim().Length>0)
            {
                if (Convert.ToInt32(tbPaymentTypeKey.Text.Trim()) > 255)
                {
                    MessageBox.Show("Key should be between 2 - 255");
                }
                else if (Convert.ToInt32(tbPaymentTypeKey.Text.Trim())<2)
                {
                    MessageBox.Show("Key should be between 2 - 255");
                }
                else
                {
                    await Globals.db.GetPaymentTypeObjects("ID=" + tbPaymentTypeKey.Text.Trim() + "");
                    if (Globals.db.PaymentTypeRecords.Count>0)
                    {
                        MessageBox.Show("Key is already exists.");
                    }
                    else
                    {
                        try
                        {
                            paymenttype.col_ID = Convert.ToByte(tbPaymentTypeKey.Text.Trim()); paymenttype.bFilterID = true;
                            paymenttype.col_Name = tbPaymentTypeValue.Text.Trim(); paymenttype.bFilterName = true;
                            await Globals.db.AddPaymentTypeObject(paymenttype);
                            Globals.configuration.init();
                            this.DialogResult = true;

                        }
                        catch (Exception) { }
                    }    
                }
            }
            else
            {
                MessageBox.Show("Please enter a valid Key Name and Value", "Warning", MessageBoxButton.OK);   
            } 
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void tbPaymentTypeKey_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = !IsNumberKey(e.Key);
        }

        private bool IsNumberKey(Key inKey)
        {
            if (inKey < Key.D0 || inKey > Key.D9)
            {
                if (inKey < Key.NumPad0 || inKey > Key.NumPad9)
                {
                    return false;
                }
            }
            return true;
        }
    }
}

